package com.editso.haier.service;

import com.editso.haier.dao.TagDao;
import com.editso.haier.entity.Tag;
import com.editso.haier.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class AdminBaseService {
    private TagDao tagDao;

    @Autowired
    public void setTagDao(TagDao tagDao) {
        this.tagDao = tagDao;
    }

    public Result addTags(List<Object> tags){
        return Result.promise(callback->{
            tagDao.saveAll(() -> tags.stream().map(tag->{
                Map<String, String> map = (Map<String, String>) tag;
                return new Tag(map.get("name"));
            }).filter(tag -> !tagDao.existsByName(tag.getName())).iterator());
        });
    }
}
